package core.proto.rightwindow;

//import java.applet.*; 
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*; 
import java.net.*; 
import java.util.*; 

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.oreilly.servlet.HttpMessage; 

import core.proto.InfoViz;
import core.proto.Util;

public class HttpChatPanel extends JPanel implements Runnable, MessageTypes {
	final static boolean shouldFill = true;
	final static boolean shouldWeightX = true;
	
	JTextArea chatDisplayArea;
	JTextField chatEntryArea; 
	Thread chatThread; 
	String user = InfoViz.UserID; 

	public HttpChatPanel() { 
		// Set up the user interface... 
		// On top, a large TextArea showing what everyone's saying. 
		// Underneath, a labeled TextField to accept this user's input.

		JLabel chatLabel = new JLabel("Chat:");
		TextHandler handler = new TextHandler();

		chatEntryArea = new JTextField(20);
		chatEntryArea.addKeyListener(handler);

		chatDisplayArea = new JTextArea(10,15);
		chatDisplayArea.setEditable(false);
		chatDisplayArea.setDragEnabled(true);

		JScrollPane cArea = new JScrollPane(chatDisplayArea);
		
		// Use a grid bag layout for our chat panel. 
		GridBagLayout gridBag = new GridBagLayout();
		this.setLayout(gridBag);
		GridBagConstraints gridCons = new GridBagConstraints();
		if (shouldFill) {
			//natural height, maximum width
			gridCons.fill = GridBagConstraints.HORIZONTAL;
		}
		if (shouldWeightX) {
			gridCons.weightx = 0.1;
		}

		// label tells user this is a label. 
		gridCons.gridx = 0;
		gridCons.gridy = 0;
		gridCons.weightx = 0.1;
		this.add(chatLabel, gridCons);

		// text area for them to enter the text. 
		gridCons.fill = GridBagConstraints.HORIZONTAL;
		gridCons.gridwidth = 2;
		gridCons.weightx = 1.0;
		gridCons.gridx = 1;
		gridCons.gridy = 0;
		this.add(chatEntryArea, gridCons);

		gridCons.fill = GridBagConstraints.HORIZONTAL;
		gridCons.ipady = 600;
		gridCons.gridwidth = 3;
		gridCons.gridx = 0;
		gridCons.gridy = 1;
		gridCons.weightx = 1.0;
		gridCons.weighty = 1.0;
		this.add(cArea, gridCons);

		if (user == null) user = "Anonymous";
		this.start();
	} 

	public void start() { 
		chatThread = new Thread(this); 
		chatThread.start(); 
	} 

	public String getNextMessage() { 
		String nextMessage = null; 
		while (nextMessage == null) { 
			try { 
				InputStream in = WebUtil.pullChatMessage();

				BufferedReader data = new BufferedReader(new InputStreamReader(in)); 
				nextMessage = data.readLine(); 
				System.out.println(nextMessage);
			} catch (SocketException e) { 
				// Can't connect to host, report it and wait before trying again 
				System.out.println("Can't connect to host: " + e.getMessage()); 
				try { 
					Thread.sleep(5000); 
				} catch (InterruptedException ignored) { } 
			} catch (FileNotFoundException e) { 
				// Servlet doesn't exist, report it and wait before trying again 
				System.out.println("Resource not found: " + e.getMessage()); 
				try { 
					Thread.sleep(5000); 
				} catch (InterruptedException ignored) { } 
			} catch (Exception e) { // Some other problem, report it and wait before trying again 
				System.out.println("General exception: " + e.getClass().getName() + ": " + e.getMessage()); 
				try { 
					Thread.sleep(1000); 
				} catch (InterruptedException ignored) { } } 
		} return nextMessage + "\n"; 
	} 

	public void run() { 
		while (true) { 
			chatDisplayArea.append(getNextMessage());
			repaint();
		} 
	} 

		
	// Sends out message to the other people. 
	void broadcastMessage(String message) { 
		message = user + ": " + message; 
		// Pre-pend the speaker's name 
		try { 
			WebUtil.sendChatMessage(message);			 
		} catch (SocketException e) { 
			// Can't connect to host, report it and abandon the broadcast 
			System.out.println("Can't connect to host: " + e.getMessage()); 
		} catch (FileNotFoundException e) { 
			// Servlet doesn't exist, report it and abandon the broadcast 
			System.out.println("Resource not found: " + e.getMessage()); 
		} catch (Exception e) { 
			// Some other problem, report it and abandon the broadcast 
			System.out.println("General exception: " + e.getClass().getName() + ": " + e.getMessage());
		} 
	} 

	private class TextHandler extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			String text = chatEntryArea.getText();
			if(e.getKeyCode() == 10 && !text.equals("")) {
				chatEntryArea.setText("");
				broadcastMessage(text);
			}
		}
	}
}